package com.sdry.web.controller.workplace;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.sdry.model.hpf.Bom;
import com.sdry.model.hpf.LineSideLibrary;
import com.sdry.model.hpf.LineSideLibraryOut;
import com.sdry.model.hpf.Part;
import com.sdry.model.hpf.Product;
import com.sdry.model.workplace.Assemble;
import com.sdry.model.workplace.AssembleExample;
import com.sdry.model.workplace.Centrifugal;
import com.sdry.model.workplace.CentrifugalExample;
import com.sdry.model.workplace.MousseBundleSample;
import com.sdry.model.workplace.AssembleExample.Criteria;
import com.sdry.service.hpf.BomService;
import com.sdry.service.hpf.LineSideLibraryService;
import com.sdry.service.hpf.PartService;
import com.sdry.service.hpf.ProductService;
import com.sdry.service.workplace.AssembleServise;
import com.sdry.service.workplace.CentrifugalServise;
import com.sdry.service.workplace.MousseBundleSampleServise;
import com.sdry.service.workplace.MousseSampleServise;
import com.sdry.service.workplace.OthersService;
import com.sdry.utils.ResponseUtil;

/**
 * @ClassName: OthersController
 * @Description: 生产工位相关
 * @author tdd
 * @date 2018年8月29日
 * @version V1.0
 */
@Controller
@RequestMapping("/revoke")
public class AssembleRevokeController {
	//组装
	@Resource
    private AssembleServise assembleServise;
	//离心机
	@Resource
    private CentrifugalServise centrifugalServise;
	//膜丝
	@Resource
    private MousseBundleSampleServise mousseBundleSampleServise;
	//产品
	@Resource
    private ProductService productServise;
	//BOM
	@Resource
    private BomService bomServise;
	//线边库
	@Resource
    private LineSideLibraryService lineSideLibraryService;
	//配件
	@Resource
    private PartService partService;

	@Resource
	 private OthersService othersService;

	 /**
	  * 组装
	  * @param response
	  * @param assemble
	  * @throws Exception
	  */
    @RequestMapping("/assemble")
    public void assemble(HttpServletResponse response,Assemble assemble) throws Exception {
        //返回标识   0 失败   -1 已经在下一工位  -2已经删除   1成功
        int affact = 0;
        
        //根据膜管查是否已经在离心工位
        CentrifugalExample centrifugalExample = new CentrifugalExample();
        com.sdry.model.workplace.CentrifugalExample.Criteria createCriteria2 = centrifugalExample.createCriteria();
        //名称查询
        createCriteria2.andCtubeEqualTo(assemble.getAtube());
        //是否报废   1是   0否
        createCriteria2.andCremark1EqualTo("0");
        List<Centrifugal> centrifugals = centrifugalServise.selectByExample(centrifugalExample);
        if(centrifugals.size() > 0){
        	//已经在下一工位 
        	affact = -1;
        }else{
        	//根据膜管查在组装工位是否存在
        	AssembleExample example = new AssembleExample();
            Criteria createCriteria = example.createCriteria();
            createCriteria.andAtubeEqualTo(assemble.getAtube());
            createCriteria.andAremarkEqualTo("0");
            List<Assemble> assembles = assembleServise.selectByExample(example);
            if(assembles.size() <= 0){
            	//已经删除
            	affact = -2;
            }else{
            	//组装id
            	List<String> idList = new ArrayList<>();
            	//组装膜丝id
            	List<Long> msLinst = new ArrayList<>();
            	for (int i = 0; i < assembles.size(); i++) {
            		idList.add(String.valueOf(assembles.get(i).getAid()));
            		msLinst.add(Long.valueOf(assembles.get(i).getMbcode()));
				}
            	//删除组装添加的信息
            	String ids = String.join(",", idList);
            	assembleServise.deleteByPrimaryKeys(ids);
            	
            	//把组装的膜丝从逻辑删除改为未删除
            	for (int i = 0; i < msLinst.size(); i++) {
        			 MousseBundleSample mousseBundleSample = new MousseBundleSample();
        			 mousseBundleSample.setMbsid(msLinst.get(i));
        			 mousseBundleSample.setMbsremark1("0");
        			 mousseBundleSampleServise.updateByPrimaryKeySelective(mousseBundleSample);
       			}
            	
            	//根据膜管编号查询膜管组装
            	Map<String, Object> map = new HashMap<>();
        		map.put("atube", assemble.getAtube());
        		map.put("atime", "");
        		map.put("aremark5", "");
        		map.put("product", "");
        		//是否删除
        		map.put("aremark", "");
        		List<Assemble> assems = othersService.selectAtubeByDateAndProduct(map);
        		//根据删除组装添加的信息后是否有该膜管，判断是否是二次组装
        		//如果是
        		if(assems.size() > 0){
        			//获取此组装的前一次组装即二次组装覆盖的组装时间
        			String time = assems.get(0).getAtime();
        			//根据操作时间获取信息
        			AssembleExample example2 = new AssembleExample();
                    Criteria createCriteria22 = example2.createCriteria();
                    createCriteria22.andAtimeEqualTo(time);
                    List<Assemble> erCassembles = assembleServise.selectByExample(example);
                    //将二次绑定的覆盖信息返回
                    for (int i = 0; i < erCassembles.size(); i++) {
                    	Assemble updateassemble = erCassembles.get(i);
	           	   		updateassemble.setAremark("0");
	           	   		assembleServise.updateByPrimaryKeySelective(assemble);
					}
        		}
        		//线边库入库
        		//1、根据产品条码判定编码规则
        		String productNumber = "";
        		String atube = assemble.getAtube().substring(3, 7);
        		if("0960".equals(atube)){
        			productNumber = "PSH2-0960-T-01";
        		}else if("0980".equals(atube)){
        			productNumber = "PSH2-0980-T-02";
        		}else if("1080".equals(atube)){
        			productNumber = "PSH1-1080-C-04";
        		}else if("1060".equals(atube)){
        			productNumber = "PSH1-1060-B-03";
        		}
        		//2、找到对应的BOM值
        		List<Product> list = productServise.getProductByNumber(productNumber);
        		Product product = list.get(0);
        		List<Bom> bomList = bomServise.getBomByPid(product.getPid());
        		//3、调用线边库入库的方法
        		for(Bom bom : bomList){
        			Part part = partService.getPartByPaid(bom.getPaid());
        			if("组装".equals(part.getParemark4())){
        				LineSideLibraryOut lineSideLibraryOut = lineSideLibraryService.findLineSideLibraryOutByPaID(bom.getPaid());
         				if(lineSideLibraryOut != null){
         					lineSideLibraryService.twoInsert(bom.getPaid(), lineSideLibraryOut.getBatch(), bom.getNumber(), assemble.getAplan());
         				}
        			}
        		}
            	affact = 1;
            }
        }
        ResponseUtil.write(response, affact);
    }

}
